package com.heima.test;

import java.io.File;
import java.io.IOException;

import javax.print.Doc;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class Demo {
	@Test
	public void demo1 ()throws Exception{
		//指定索引库的存放路径
		Directory directory = FSDirectory.open(new File("F:\\index"));
		//创建一个标准分析器
		Analyzer analyzer = new StandardAnalyzer();
		//创建indexWriterConfig对象
		//第一个参数:Lucene的版本信息,可以选择对应的lucene版本
		//第二个参数:分析器对象
		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST, analyzer);
		//创建indexWriter对象
		IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
		//原始文档的路径
		File dir = new File("C:\\Users\\Administrator\\Desktop\\自己笔记");
		for (File file : dir.listFiles()) {
			//获取文件名
			String name = file.getName();
			//获取文件内容
			String content = FileUtils.readFileToString(file);
			//文件路径
			String path = file.getPath();
			//文件大小
			long sizeOf = FileUtils.sizeOf(file);
			//创建文件名域
			Field filenameField = new TextField("filename", name, Store.YES);
			Field fileContentField =  new TextField("filecontent", content, Store.YES);
			Field filePath = new StoredField("filePath", path);
			Field fileSize = new LongField("fileSize", sizeOf, Store.YES);
			Document doc = new Document();
			doc.add(fileContentField);
			doc.add(filenameField);
			doc.add(fileSize);
			doc.add(filePath);
			indexWriter.addDocument(doc);
		}
		indexWriter.close();
	}
	@Test
	public void demo2() throws Exception{
		//指定索引存放的路径
		Directory directory = FSDirectory.open(new File("F:\\index"));
		//创建indexReader对象
		IndexReader indexReader = DirectoryReader.open(directory);
		//创建indexSearch对象
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);
		//创建查询
		Query query = new TermQuery(new Term("name", "bb"));
		//执行查询
		TopDocs topDocs = indexSearcher.search(query, 10);
		System.out.println("总条数:"+topDocs.totalHits);
		//遍历查询结果
		for(ScoreDoc s : topDocs.scoreDocs){
			Document doc = indexSearcher.doc(s.doc);
			/*System.out.println(doc.get("filename"));
			System.out.println(doc.get("filecontent"));
			System.out.println(doc.get("filePath"));
			System.out.println(doc.get("fileSize"));*/
			System.out.println(doc.get("name"));
			System.out.println(doc.get("id"));
			
		}
		//关闭资源
		indexReader.close();
	}
	@Test
	public void demo3() throws Exception{
		//创建一个标准分析器
		Analyzer analyzer = new IKAnalyzer();
		//获得tokenStream对象
		//TokenStream tokenStream = analyzer.tokenStream("test","The Spring Framework provides a comprehensive programming and configuration model.");
		TokenStream tokenStream = analyzer.tokenStream("test","我是中国人");
		
		//添加一个引用,可以获得每个关键词
		CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
		//添加一个偏移量的引用,记录关键词的开始位置
		OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
		//把指针调整到列表的头部
		tokenStream.reset();
		while (tokenStream.incrementToken()) {
			//关键词的起始位置
			System.out.println("start:"+offsetAttribute.startOffset());
			//取关键词
			System.out.println(charTermAttribute);
			//结束位置
			System.out.println("end:"+offsetAttribute.endOffset());
		}
		tokenStream.close();
	}
	//索引库的添加
	@Test
	public void demo4() throws Exception{
		Directory directory = FSDirectory.open(new File("F:\\index"));
		Analyzer analyzer = new IKAnalyzer();
		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST, analyzer);
		IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
		Document document = new Document();
		document.add(new TextField("name", "bb", Store.YES));
		document.add(new StringField("id", "002", Store.YES));
		indexWriter.addDocument(document);
		indexWriter.close();
	}
	//索引库的删除,删除所有
	@Test
	public void demo5() throws Exception{
		IndexWriter indexWriter = getIndexWriter();
		indexWriter.deleteAll();
		indexWriter.close();
	}
	//索引库的删除,按条件删除
	@Test
	public void demo6() throws Exception{
		IndexWriter indexWriter = getIndexWriter();
		Query query = new TermQuery(new Term("name","aa"));
		indexWriter.deleteDocuments(query);
		indexWriter.close();
	}
	//索引库的更新
	@Test
	public void demo7() throws Exception{
		IndexWriter indexWriter = getIndexWriter();
		Document document = new Document();
		document.add(new TextField("name", "wehwewej", Store.YES));
		document.add(new TextField("id", "009",Store.YES));
		indexWriter.updateDocument(new Term("name","bb"), document);
		indexWriter.close();
	}
	
	public IndexWriter getIndexWriter() throws Exception{
		Directory directory = FSDirectory.open(new File("F:\\index"));
		Analyzer analyzer = new IKAnalyzer();
		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST, analyzer);
		return new IndexWriter(directory, indexWriterConfig);
	}
}
